package com.zhangkeyou.map.mapweb.mapper.handler;

import com.alibaba.fastjson.JSON;
import com.zhangkeyou.map.mapweb.domain.entity.Feature;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GeometryTypeHandler extends BaseTypeHandler<Feature.Geometry> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Feature.Geometry geometry, JdbcType jdbcType) throws SQLException {
        String result = JSON.toJSONString(geometry);
        preparedStatement.setString(i,result);
    }

    @Override
    public Feature.Geometry getNullableResult(ResultSet resultSet, String s) throws SQLException {
        Feature.Geometry geometry = JSON.parseObject(resultSet.getString(s), Feature.Geometry.class);
        return geometry;
    }

    @Override
    public Feature.Geometry getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return null;
    }

    @Override
    public Feature.Geometry getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return null;
    }
}
